package net.com.sunkey.messagehandler;

import java.nio.ByteBuffer;

import net.com.sunkey.protobuf.JoinRoomSucceedResponse.JoinRoomSucceedResponseData;
import net.com.sunkey.protobuf.Protocol;
import net.com.sunkey.protobuf.RoomData;
import net.com.sunkey.serverlogic.Room;
import net.com.sunkey.serverlogic.Server;

/**
 * @author kohachiro
 *
 */
public class JoinRoomSucceedResponseHandler {
	/* (non-Javadoc)
	 * @see com.sunkey.tdserver.messagehandler.MessageHandler#invoke(java.lang.Object, org.jboss.netty.channel.Channel)
	 */
	//@Override
	public static int invoke(Object data, Object channel) throws Exception {
		System.out.println("["+channel.hashCode()+"]["+Protocol.MessageHandler.JoinRoomSucceedResponse_VALUE+"]JoinRoomSucceedResponse");
		Room room=(Room)data;
		
		RoomData.Room.Builder roomBuilder=RoomData.Room.newBuilder();	
		roomBuilder.setId(room.getId());
		roomBuilder.setName(room.getName());
		roomBuilder.setMaxUsers(room.getMaxUsers());
		if (room.getPassword()=="")
			roomBuilder.setNeedpassword(false);
		else
			roomBuilder.setNeedpassword(true);
		room.addProperties(roomBuilder);
		
		JoinRoomSucceedResponseData.Builder builder=JoinRoomSucceedResponseData.newBuilder();
		builder.setRoom(roomBuilder);
		JoinRoomSucceedResponseData message=builder.build();
		
		Server.sendResponse(Protocol.MessageHandler.JoinRoomSucceedResponse_VALUE, ByteBuffer.wrap(message.toByteArray()), channel);

		return Protocol.MessageHandler.JoinRoomSucceedResponse_VALUE;
	}

}
